package com.rickpan.dto.share;

import com.rickpan.entity.FileShare;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 创建分享请求DTO
 * 
 * @author RickPan Team
 * @since 2025-01-05
 */
public class CreateShareRequest {

    /**
     * 文件ID
     */
    @NotNull(message = "文件ID不能为空")
    private Long fileId;

    /**
     * 分享类型
     */
    @NotNull(message = "分享类型不能为空")
    private FileShare.ShareType shareType = FileShare.ShareType.PUBLIC;

    /**
     * 访问密码 (当shareType为PASSWORD时必填)
     */
    private String password;

    /**
     * 允许下载
     */
    private Boolean allowDownload = true;

    /**
     * 允许预览
     */
    private Boolean allowPreview = true;

    /**
     * 过期时间
     */
    private LocalDateTime expireTime;

    /**
     * 最大访问次数
     */
    @Min(value = 1, message = "最大访问次数必须大于0")
    private Integer maxAccessCount;

    /**
     * 分享备注/说明
     */
    @Size(max = 500, message = "分享备注长度不能超过500个字符")
    private String description;

    // 构造函数
    public CreateShareRequest() {}

    // Getter 和 Setter 方法
    public Long getFileId() { return fileId; }
    public void setFileId(Long fileId) { this.fileId = fileId; }

    public FileShare.ShareType getShareType() { return shareType; }
    public void setShareType(FileShare.ShareType shareType) { this.shareType = shareType; }

    public String getPassword() { return password; }
    public void setPassword(String password) { this.password = password; }

    public Boolean getAllowDownload() { return allowDownload; }
    public void setAllowDownload(Boolean allowDownload) { this.allowDownload = allowDownload; }

    public Boolean getAllowPreview() { return allowPreview; }
    public void setAllowPreview(Boolean allowPreview) { this.allowPreview = allowPreview; }

    public LocalDateTime getExpireTime() { return expireTime; }
    public void setExpireTime(LocalDateTime expireTime) { this.expireTime = expireTime; }

    public Integer getMaxAccessCount() { return maxAccessCount; }
    public void setMaxAccessCount(Integer maxAccessCount) { this.maxAccessCount = maxAccessCount; }

    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }

    /**
     * 验证请求参数
     */
    public void validate() {
        // 如果是密码保护类型，密码不能为空且长度要符合要求
        if (shareType == FileShare.ShareType.PASSWORD) {
            if (password == null || password.trim().isEmpty()) {
                throw new IllegalArgumentException("密码保护分享必须设置密码");
            }
            if (password.length() < 4 || password.length() > 20) {
                throw new IllegalArgumentException("密码长度必须在4-20位之间");
            }
        }

        // 如果设置了过期时间，必须是未来时间
        if (expireTime != null && expireTime.isBefore(LocalDateTime.now())) {
            throw new IllegalArgumentException("过期时间必须是未来时间");
        }

        // 至少要允许预览或下载其中一种权限
        if (!Boolean.TRUE.equals(allowPreview) && !Boolean.TRUE.equals(allowDownload)) {
            throw new IllegalArgumentException("至少要允许预览或下载其中一种权限");
        }
    }
}
